<!--
 * @Author: cqg
 * @Date: 2021-06-01 14:13:52
 * @LastEditTime: 2021-06-03 11:09:47
 * @Description: 复查弹窗
 * @LastEditors: cqg
-->
<template>
  <div>
    <el-dialog class="basic-from" title="复查" :visible.sync="dialogVisible">
      <el-form :model="form" ref="form" class="form-wrap" style="padding-top:0px;" :rules="rules">
        <el-form-item label="复查人" prop="approvePerson">
          <el-input v-model="form.approvePerson" readonly placeholder="请输入" />
        </el-form-item>
        <el-form-item label="复查参与人员" prop="inspectionOtherPerson">
          <el-input v-model="form.inspectionOtherPerson" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="复查时间" prop="approveDate">
          <el-date-picker value-format="yyyy-MM-dd" v-model="form.approveDate" disabled type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="复查结果" prop="inspectionResult">
          <el-select clearable v-model="form.inspectionResult" filterable placeholder="-=请选择=-">
            <el-option label="复查达标" :value="3" />
            <el-option label="复查不达标" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="复查意见" class="full" prop="opinion">
          <el-input v-halfCharCode v-model="form.opinion" type="textarea" maxlength="10" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="附件" class="full" v-if="dialogVisible">
          <v-upload type="file" ref="upload" :busId="form.inspectionLogId" modelType="review" @onSuccess="onSuccess" v-model="uploadFileList" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-throttle="[sumbit]">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
let moment = require("moment");
import { mapState } from "vuex";
import {
  InspectionLog
} from "../../../api/check-in.js";
export default {
  name: 'reviewDialog',
  inject: ['initList'],
  computed: mapState({
    user: state => state.user.user
  }),
  data () {
    return {
      uploadFileList: [],
      dialogVisible: false,
      form: {
        resultId: '',
        inspectionLogId: '',
        approvePerson: '',
        inspectionOtherPerson: '',
        approveDate: moment().format("yyyy-MM-DD"),
        inspectionResult: '',
        opinion: ''
      },
      rules: {
        inspectionResult: [
          {
            required: true,
            message: "请选择复查结果",
            trigger: "blur"
          }
        ],
        opinion: [
          {
            required: true,
            message: "请输入复查意见",
            trigger: "blur"
          }
        ],
      }
    }
  },
  methods: {
    showDialog (row) {
      this.dialogVisible = true
      this.form = {
        resultId: row.resultId,
        inspectionLogId: '',
        approvePerson: this.user.realName,
        inspectionOtherPerson: '',
        approveDate: moment().format("yyyy-MM-DD"),
        inspectionResult: '',
        opinion: '',
      }
      this.$refs.form.clearValidate()
    },
    sumbit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.$refs.upload.submit("review", this.form.inspectionLogId);
        }
      });
    },
    // 上传文件提交
    async onSuccess (response, file, fileList) {
      let params = { ...this.form, inspectionLogId: response.data }
      const res = await InspectionLog(params);
      this.dialogVisible = false;
      this.$message.success('保存成功');
      this.initList();
    },
  },
}
</script>
